/**
 * Copyright (c) 2020, Mr.Wang (recallcode@aliyun.com) All rights reserved.
 */

package com.cpipec.demo.spi.impl;

import com.cpipec.demo.spi.BPMServerRestClient;
import com.cpipec.demo.vo.TrainVO;
import com.spic.common.server.ServerUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Component
public class BPMServerRestClientImpl implements BPMServerRestClient {

	private static Logger logger = LoggerFactory.getLogger(BPMServerRestClientImpl.class);

	@Value("${eos.system-id}")
	private String clientSysCode;

	@Value("${eos.server-http}")
	private String serverHttp;

	@Value("${eos.server-token}")
	private String serverToken;

	@Value("${eos.bpm.code}")
	private String bpmCode;

	@Value("${eos.bpm.secret}")
	private String bpmSecret;

	@Value("${security.iam.cookie.cookie-name-suffix}")
	private String cookieSuffixName;

	@Autowired
	private RestTemplate restTemplate;

	@Override
	public String launchSendTask(TrainVO trainVO) {
		HttpHeaders requestHeaders = ServerUtils.createHeader(cookieSuffixName, serverToken);
		Map<String, Object> bpmMap = new HashMap<String, Object>();
		Map<String, Object> taskData = new HashMap<String, Object>();
		Map<String, Object> strVars = new HashMap<String, Object>();
		bpmMap.put("systemCode", bpmCode);
		bpmMap.put("systemSecret", bpmSecret);
		bpmMap.put("option", "SPIC_LaunchSendTask");
		taskData.put("strProcessName", "培训项目立项审批单");
		taskData.put("strUltUserCode", trainVO.getCreateName());
		strVars.put("money", trainVO.getTrainCost());
		strVars.put("PRGID", "train_project");
		strVars.put("PKVALUE", trainVO.getId());
		strVars.put("TASKSHT", trainVO.getProjectName());
		strVars.put("TABLE", trainVO.getCreateName());
		strVars.put("FORMURL", trainVO.getUserName());
		taskData.put("strVars", strVars);
		bpmMap.put("taskData", taskData);
		HttpEntity<Map> requestEntity = new HttpEntity<Map>(bpmMap, requestHeaders);
		ResponseEntity<String> response = restTemplate.exchange(serverHttp + "/bpm/spic_launchsendtask", HttpMethod.POST, requestEntity, String.class);
		return response.getBody();
	}

	@Override
	public String getIncidentInfo(String instanceId) {
		HttpHeaders requestHeaders = ServerUtils.createHeader(cookieSuffixName, serverToken);
		Map<String, Object> bpmMap = new HashMap<String, Object>();
		Map<String, Object> taskData = new HashMap<String, Object>();
		bpmMap.put("systemCode", bpmCode);
		bpmMap.put("systemSecret", bpmSecret);
		bpmMap.put("option", "SPIC_GetIncidentInfo");
		taskData.put("strProcessName", "培训项目立项审批单");
		taskData.put("nIncident", Integer.valueOf(instanceId).intValue());
		bpmMap.put("taskData", taskData);
		HttpEntity<Map> requestEntity = new HttpEntity<Map>(bpmMap, requestHeaders);
		ResponseEntity<String> response = restTemplate.exchange(serverHttp + "/bpm/spic_getincidentinfo", HttpMethod.POST, requestEntity, String.class);
		return response.getBody();
	}

	@Override
	public String getGetGraphicalStatus(String instanceId) {
		HttpHeaders requestHeaders = ServerUtils.createHeader(cookieSuffixName, serverToken);
		Map<String, Object> bpmMap = new HashMap<String, Object>();
		Map<String, Object> taskData = new HashMap<String, Object>();
		bpmMap.put("systemCode", bpmCode);
		bpmMap.put("systemSecret", bpmSecret);
		bpmMap.put("option", "SPIC_GetGraphicalStatus");
		taskData.put("strProcessName", "培训项目立项审批单");
		taskData.put("nIncident", Integer.valueOf(instanceId).intValue());
		bpmMap.put("taskData", taskData);
		HttpEntity<Map> requestEntity = new HttpEntity<Map>(bpmMap, requestHeaders);
		ResponseEntity<String> response = restTemplate.exchange(serverHttp + "/bpm/spic_getgraphicalstatus", HttpMethod.POST, requestEntity, String.class);
		return response.getBody();
	}

	@Override
	public String getPendingTaskList(String username, int page, int pageSize) {
		HttpHeaders requestHeaders = ServerUtils.createHeader(cookieSuffixName, serverToken);
		Map<String, Object> bpmMap = new HashMap<String, Object>();
		Map<String, Object> paraInfo = new HashMap<String, Object>();
		Map<String, Object> tWhere = new HashMap<String, Object>();
		bpmMap.put("systemCode", bpmCode);
		bpmMap.put("systemSecret", bpmSecret);
		bpmMap.put("option", "GetPendingTaskList");
		paraInfo.put("pageIndex", page);
		paraInfo.put("pageSize", pageSize);
		tWhere.put("PENDINGUSERID", username);
		paraInfo.put("TWhere", tWhere);
		bpmMap.put("paraInfo", paraInfo);
		HttpEntity<Map> requestEntity = new HttpEntity<Map>(bpmMap, requestHeaders);
		ResponseEntity<String> response = restTemplate.exchange(serverHttp + "/bpm/getpendingtasklist", HttpMethod.POST, requestEntity, String.class);
		return response.getBody();
	}

	@Override
	public String submitTaskStep(TrainVO trainVO) {
		HttpHeaders requestHeaders = ServerUtils.createHeader(cookieSuffixName, serverToken);
		Map<String, Object> bpmMap = new HashMap<String, Object>();
		Map<String, Object> taskData = new HashMap<String, Object>();
		bpmMap.put("systemCode", bpmCode);
		bpmMap.put("systemSecret", bpmSecret);
		bpmMap.put("option", "SPIC_SubmitTaskStep");
		taskData.put("strTaskId", trainVO.getTaskId());
		taskData.put("strUltUserCode", "bpm/" + trainVO.getTaskUser());
		bpmMap.put("taskData", taskData);
		HttpEntity<Map> requestEntity = new HttpEntity<Map>(bpmMap, requestHeaders);
		ResponseEntity<String> response = restTemplate.exchange(serverHttp + "/bpm/spic_submittaskstep", HttpMethod.POST, requestEntity, String.class);
		return response.getBody();
	}

	@Override
	public String getCompleteTaskList(String username, int page, int pageSize) {
		HttpHeaders requestHeaders = ServerUtils.createHeader(cookieSuffixName, serverToken);
		Map<String, Object> bpmMap = new HashMap<String, Object>();
		Map<String, Object> paraInfo = new HashMap<String, Object>();
		Map<String, Object> tWhere = new HashMap<String, Object>();
		bpmMap.put("systemCode", bpmCode);
		bpmMap.put("systemSecret", bpmSecret);
		bpmMap.put("option", "GetCompleteTaskList");
		paraInfo.put("pageIndex", page);
		paraInfo.put("pageSize", pageSize);
		tWhere.put("PENDINGUSERID", username);
		paraInfo.put("TWhere", tWhere);
		bpmMap.put("paraInfo", paraInfo);
		HttpEntity<Map> requestEntity = new HttpEntity<Map>(bpmMap, requestHeaders);
		ResponseEntity<String> response = restTemplate.exchange(serverHttp + "/bpm/getcompletetasklist", HttpMethod.POST, requestEntity, String.class);
		return response.getBody();
	}
}
